/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rallyracer;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.util.Comparator;
import java.util.List;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 *
 * @author samra_000
 */
public class DataPoint implements Comparator<DataPoint>
{
    //fixed at start
    final int serialNumber;
    float distanceFromStart;
    float recommendedSpeed; 
    int pause;
    
    //result data
    float time;
    DateTime absoluteTime;
    public static final String[] header = {"SerialNumber","RecommendedSpeed","AbsoluteTime","StopWatchTime","Miles"};
    
    public static final DateTimeFormatter dateFormatter = DateTimeFormat.mediumTime();
    
    public DataPoint( int serialNumber )
    {
        this.serialNumber = serialNumber;
        this.distanceFromStart = -1;
        this.recommendedSpeed = -1;
        this.time = -1;
        this.absoluteTime = new DateTime();
        this.pause = 0;
    }
    
    /**
     * 
     * @param recommendedSpeed in MPH
     */
    public void setRecommendedSpeed( float recommendedSpeed )
    {
        this.recommendedSpeed = recommendedSpeed;
    }
    
    @Override
    public String toString()
    {
        StringBuilder str = new StringBuilder();
        Joiner.on(", ").appendTo(str, this.serialNumber, this.recommendedSpeed, this.distanceFromStart, this.pause, this.time );
        return str.toString();
    }
    
    public String[] getCSVContent()
    {
        List<String> csvContent = Lists.newArrayList();
        //{"SerialNumber","RecommendedSpeed","AbsoluteTime","TimeTillNextSpeedChange","Miles"};
        csvContent.add(Integer.toString(this.serialNumber));
        csvContent.add(Float.toString(this.recommendedSpeed));
        csvContent.add(this.getFormattedAbsoluteTime());
        csvContent.add(Float.toString(this.time));
        csvContent.add(Float.toString(this.distanceFromStart));
        return  csvContent.toArray(new String[csvContent.size()]);
    }
    
    
    
    @Override
    public int compare(DataPoint o1, DataPoint o2) 
    {
        return o1.serialNumber - o2.serialNumber;
    }

    private String getFormattedAbsoluteTime() 
    {
        if( this.absoluteTime!=null )
        {
            return this.dateFormatter.print(this.absoluteTime);
        }
        return "";
    }
}
